<?php
namespace jy_newpingfee;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	分销插件所有金额字段均保存整形，精确到分。显示时，除以100然后保留两位小数即可
	佣金比例字段保存整形，值为万分数，相当于百分数的精确到小数点后2位。即展示到前端时，需要除以100，换算成百分数显示
*/
class member extends \plugin{
	public function checklogin($storeid='',$area_agentid='',$area_code=''){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json'];
			model('core/core')->checklogin();
		}else{
			model('jy_weishop/member')->checklogin();
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
			if(empty($member)){
				model('core/member')->logout();
				model('core/core')->checklogin('jy_newpingfee/myhome/');
			}else{
				$user=pdo_get('jy_newpingfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'fromuser'=>$_SESSION['fromuser']?:$member['fromuser'],
						'uniacid'=>$_SESSION['uniacid'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_newpingfee_member',$insert);
					if($insert['fromuser']){
						$this->update_agent_level($insert['fromuser']);
						//上上级升级
						$shfromuser=pdo_getcolumn('jy_newpingfee_member',array('mid'=>$insert['fromuser'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
						if($shfromuser){
							$this->update_agent_level($shfromuser);
						}
					}
				}
			}
		}
	}
	public function setfromuser($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$fromuser=pdo_getcolumn('jy_newpingfee_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if(!$fromuser && $_SESSION['fromuser'] && $_SESSION['fromuser'] !=$mid){
			pdo_update('jy_newpingfee_member',array('fromuser'=>$_SESSION['fromuser']),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$this->update_agent_level($fromuser);
			//上上级升级
			$shfromuser=pdo_getcolumn('jy_newpingfee_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
			if($shfromuser){
				$this->update_agent_level($shfromuser);
			}
		}
		return false;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($fromuser,$self=false,$pinggoods=array()){
		$agent=$this->get('member',array('mid'=>$fromuser));
		if(!$agent['mid'] && !$self){
			return;
		}
		$level=$this->get('level',array('id'=>$agent['level_id']));
		$level['level']=$level['level']?:0;
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);
		$levels=$this->get_list('level',$parameter);
		$next_level=$levels[0];
		if(!$next_level['id']){
			return false;
		}
		$conditions=json_decode($next_level['conditions'],true);
		$level_up_types=$this->model('model')->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$up=true;
		foreach($level_up_types as $type){
			if($type=='fennum'){
				//推荐N人参与
				$downmids=pdo_getall('jy_newpingfee_member',array('fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
				if($downmids){
						$downmids=array_keys($downmids);
						$canyus=pdo_getnorepeat('jy_newpingfee_log',array('status'=>1,'type'=>0,'mid' => $downmids),'mid');
						$downnum=count($canyus);
				}
				if($downnum<$conditions['fennum'] && $conditions['fennum']){
					$up=false;
				}
			}elseif($type=='level_num'){
				//指定等级人数
				if($conditions['level_id']){
					$agent['level_num']=pdo_count('jy_newpingfee_member',array('level_id'=>$conditions['level_id']),'id');
				}
				if($agent['level_num']<$conditions['level_num'] && $conditions['level_num']){
					$up=false;
				}
			}elseif($type=='buy_goodsid' && $self){
				//购买指定的拼团商品ID，非商品ID
				if($pinggoods['id']!=$conditions['buy_goodsid'] && $conditions['buy_goodsid']){
					$up=false;
				}
			}elseif($type=='tui_num'){
				//直推人数
				$tui_num=pdo_count('jy_newpingfee_member',array('fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'id');
				if($tui_num<$conditions['tui_num'] && $conditions['tui_num']){
					$up=false;
				}
			}elseif($type=='team_num'){
				//团队人数
				$tui_num01=pdo_count('jy_newpingfee_member',array('fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'id');
				$downmids=pdo_getall('jy_newpingfee_member',array('fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
				if($downmids){
						$downmids=array_keys($downmids);
						$tui_num02=pdo_count('jy_newpingfee_member',array('fromuser'=>$downmids,'uniacid'=>$_SESSION['uniacid']),'id');
						$downmids02=pdo_getall('jy_newpingfee_member',array('fromuser'=>$downmids,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
						if($downmids02){
								$downmids02=array_keys($downmids02);
								$tui_num03=pdo_count('jy_newpingfee_member',array('fromuser'=>$downmids02,'uniacid'=>$_SESSION['uniacid']),'id');
						}
				}
				$team_num=$tui_num01+$tui_num02+$tui_num03;
				if($team_num<$conditions['team_num'] && $conditions['team_num']){
					$up=false;
				}
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('member',$update,array('mid'=>$fromuser));
			if($this->plugin_exists('jy_hehuofen') && $next_level['hehuo_id']){
				$member=array(
					'mid'=>$fromuser,
					'level_id'=>$next_level['hehuo_id'],
					'fromuser'=>$agent['fromuser'],
				);
				model('jy_hehuofen/member')->up_member($member);
			}
		}
		return true;
	}
	public function getdetail($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$user=pdo_get('jy_newpingfee_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return $user;
	}
	//type 0充值，1消费，2冻结，3解冻，4冻结消费，5退款，6提现,7返现
	public function recharge($mid=0,$fee=0,$type=0,$remark=''){
		$mid=$mid?:$_SESSION['uid'];
		if(!$fee || $fee<=0){
			return false;
		}
		$logtype=5;
		$member=pdo_get('jy_newpingfee_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$update=array();
		if($type==0){
			$update['baofee +=']=$fee;
			$msg='充值:￥'.$fee;
		}elseif($type==1){
			$update['baofee -=']=$fee;
			$msg='消费:￥'.$fee;
		}elseif($type==2){
			$update['baofee -=']=$fee;
			$update['freeze +=']=$fee;
			$msg='冻结:￥'.$fee;
		}elseif($type==3){
			$update['baofee +=']=$fee;
			$msg='解冻:￥'.$fee;
			if($member['freeze']<$fee){
				$update['freeze']=0;
			}else{
				$update['freeze -=']=$fee;
			}
		}elseif($type==4){
			$msg='参与消费解冻:￥'.$fee;
			if($member['freeze']<$fee){
				$update['freeze']=0;
			}else{
				$update['freeze -=']=$fee;
			}
		}elseif($type==5){
			$update['baofee +=']=$fee;
			$msg='退款:￥'.$fee;
		}elseif($type==6){
			$update['baofee -=']=$fee;
			$msg='提现到余额:￥'.$fee;
		}elseif($type==7){
			$update['baofee +=']=$fee;
			$msg='参与返现:￥'.$fee;
			$logtype=7;
		}
		$user=pdo_update('jy_newpingfee_member',$update,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$params=array(
			'msg'=>$msg,
			'status'=>1,
			'mid'=>$mid,
		);
		$this->model('model')->set_log($params,$logtype);
		return true;
	}
	//释放兑换余额
	public function up_tixianfee($mid=0,$iscanyu=false){
		$mid=$mid?:$_SESSION['uid'];
		$tixianset=$this->plugin_setting('tixianset');
		$tixianset['up_feerate']=$tixianset['up_feerate']?:1;
		$tixianset['up_rate']=$tixianset['up_rate']?:5;
		$man_fee=$tixianset['man_fee']?:100;
		$up_rate=$tixianset['up_rate'];
		$up_type=$tixianset['up_type'];
		if($iscanyu){
			if($tixianset['up_jiarate']){
				$up_rate=$tixianset['up_jiarate'];
			}else{
				return false;
			}
		}
		if($up_type && $tixianset['is_open']){
			if(!$iscanyu){
				$isup=pdo_get('jy_newpingfee_feelog',array('mid'=>$mid,'dayid'=>date('Y-m-d',TIMESTAMP),'type'=>0));
				if($isup['id']){
					return false;
				}
			}
			$membinfo=pdo_get('core_members',array('id'=>$mid),array('credit'.$up_type,'credit'.$up_type.'_fz'));
			$nowcredit=$membinfo['credit'.$up_type];
			$nowfzcredit=$membinfo['credit'.$up_type.'_fz'];
			if($nowfzcredit<1){
				return false;
			}
			//获取今天推荐拼团人数；
			/*if($tixianset['up_jiarate']){
				$member=pdo_get('jy_newpingfee_member',array('mid'=>$mid));
				$newdayid=date('Y-m-d',TIMESTAMP);
				if($member['dayid']==$newdayid){
					$up_rate +=$tixianset['up_jiarate']*$member['daycanyus'];
				}
			}*/
			$fzlog=pdo_getall('jy_newpingfee_fzlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'fee >'=>0));
			$upcredit=0;
			foreach($fzlog as $fv){
				if($iscanyu){
					$up_ratefee=$fv['fee']>=$fv['up_jiaratefee']?$fv['up_jiaratefee']:$fv['fee'];
				}else{
					$up_ratefee=$fv['fee']>=$fv['up_ratefee']?$fv['up_ratefee']:$fv['fee'];
				}
				$upcredit +=$up_ratefee;
				if($up_ratefee>0){
					pdo_update('jy_newpingfee_fzlog',array('fee -='=>$up_ratefee),array('id'=>$fv['id']));
				}
			}
			if($upcredit<=0){
				return false;
			}
			$upfee=intval($upcredit/$tixianset['up_feerate']);			
			model('core/member')->member_credit($mid,'credit'.$up_type.'_fz',$upcredit,false,'jy_newpingfee',$tixianset['up_feerate'].'比1，虚拟币释放到余额');
			model('core/member')->member_credit($mid,'credit2',$upfee,true,'jy_newpingfee','虚拟币释放到余额');			
			$params=array(
				'gift_type'=>$up_type,
				'fee'=>$upcredit,
				'msg'=>$tixianset['up_feerate'].'比1，虚拟币释放到余额',
				'status'=>1,
				'mid'=>$mid,
			);
			$type=$iscanyu?1:0;
			$this->model('model')->set_feelog($params,$type);
		}
		return true;
	}
}